package edu.bit.water.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import edu.bit.water.entity.Authority;
import edu.bit.water.dao.AuthorityDao;


/**
 * 这里对应数据库中Meta_assessment_indicator表的controller
 * 对应企业管理指标
 * @Author： Page
 */
@Controller
@RequestMapping("/authority")
public class AuthorityController extends BaseController{

    @Autowired
    private AuthorityDao AuthorityDao;

    @ResponseBody
    @RequestMapping(value = "/select", method=RequestMethod.GET)
    public Map<String, Object>selectAuthority(String roleId) {
        Map<String, Object> result = new HashMap<>();
        int userId = context().getUserId();
        List<Authority> list = AuthorityDao.selectAuthority();
        List<Authority> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Authority item = list.get(i);
            int temp = AuthorityDao.selectPermission(Integer.parseInt(roleId), item.getId());
            if(temp > 0) {
                list.get(i).setIsHave(1);
            }else {
                list.get(i).setIsHave(0);
            }
            //超级管理员拥有所有权限
            if(roleId.equals("0")) {
                list.get(i).setIsHave(1);
            }

            int flag = AuthorityDao.queryPermission(userId, item.getId());
            if(userId == 0 || flag > 0) {
                resultList.add(list.get(i));
            }
        }
        result.put("rows", resultList);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "update-authority", method=RequestMethod.POST)
    public Map<String, Object> updateAuthority(@RequestBody List<String> list) {
        Map<String, Object> modelMap = new HashMap<>();
        Integer roleId = Integer.parseInt(list.get(0));
        list.remove(0);
        AuthorityDao.removePerssionByRoleId(roleId);
        for (String s : list) {
            Integer id = AuthorityDao.getMaxId();
            if(id == null){
                id = 0;
            }
            else {
                id = id + 1;
            }
            boolean update = AuthorityDao.insertPermission(roleId, Integer.parseInt(s), id);
        }
        modelMap.put("status", "success");
        return modelMap;
    }


    @ResponseBody
    @RequestMapping(value = "/query-permission", method=RequestMethod.GET)
    public Map<String, Object>queryPermission(@RequestParam("authName") String authName, @RequestParam("info") String info) {
        Map<String, Object> modelMap = new HashMap<>();
        Integer userId = context().getUserId();
        if (userId == null) {
            modelMap.put("result", 0);
            return modelMap;
        }
        int count = AuthorityDao.hasPermission(userId, authName, info);
        if(userId == 0) {//超级管理员拥有所有权限
            count = 1;
        }
        modelMap.put("result", count);
        return modelMap;
    }
}
